﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace TurtleLib.Simulation
{
  public class AdvancedScreen : Screen
  {
    protected RichTextBox _rich;

    protected Panel _cover;

    public AdvancedScreen(DragablePanel parent) : base(parent)
    {
      _rich = PrepareRichTextBox();
      _rich.BorderStyle = BorderStyle.None;
      _rich.Hide();
      _rich.MouseWheel += _parent.OnMouseWheel;

      _cover = new TransparentPanel();
      _rich.SizeChanged += delegate { _cover.Size = _rich.Size; };
      _rich.LocationChanged += delegate { _cover.Location = _rich.Location; };
      _rich.ReadOnly = true;

      _cover.MouseDoubleClick += RaiseMouseDoubleClick;
      _cover.MouseDown += RaiseMouseDown;
      _cover.MouseUp += RaiseMouseUp;
      _cover.MouseMove += RaiseMouseMove;

      _parent.AddControl(_rich);
      _parent.AddControl(_cover);

      _cover.BringToFront();
    }

    private RichTextBox PrepareRichTextBox()
    {
      var rich = new RichTextBox();
      rich.BackColor = Color.Black;
      rich.ReadOnly = true;
      rich.ScrollBars = RichTextBoxScrollBars.None;
      rich.WordWrap = false;

      return rich;
    }

    public override Rectangle Resize(DragablePanel.ResizingArgs arg)
    {
      _rich.Hide();
      return base.Resize(arg);
    }

    protected override void RenderText(Graphics g, Rectangle area)
    {
      _rich.Bounds = area;
      _rich.Show();
      _rich.Font = new Font(_font.FontFamily, _font.Size - 4f);
      _rich.Clear();

      int lastY = area.Top;
      foreach (var termLine in _textBuffer)
      {
        //int lastX = area.Left + 2;
        //int largestY = 0;
        foreach (var commonSegment in termLine.GetSegments())
        {
          Color fontColor = WorldUx.ConvertColor(commonSegment.ForeColor);
          Color backColor = WorldUx.ConvertColor(commonSegment.BackgroundColor);
          string text = commonSegment.Text;

          _rich.SelectionBackColor = backColor;
          _rich.SelectionColor = fontColor;
          _rich.AppendText(text);

          //foreach (char ch in text)
          //{
          //  var endPoint = WriteColoredText(g, ch.ToString(), _font.Size, new Point(lastX, lastY), fontColor, backColor);
          //  lastX += endPoint.X;
          //  largestY = Math.Max(largestY, endPoint.Y);
          //}
        }

        _rich.AppendText("\n");
        //lastY += largestY;
      }

      _rich.SelectionStart = 0;
    }

    protected override Rectangle GetTextBoxBounds(string text)
    {
      var rich = PrepareRichTextBox();
      rich.Font = new Font(_font.FontFamily, _font.Size - 4f);

      rich.AppendText(text);

      var rect = new Rectangle(Location, rich.GetPreferredSize(new Size()));
      rect.Size = new Size(rect.Width + 5, rect.Height + 5);

      return rect;
    }

    protected override bool ValidFontSize(float size)
    {
      return base.ValidFontSize(size) && size > 4f;
    }
  }
}
